//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package rent.hj.videoplayer.subTitle.autodetect;

public abstract class nsPSMDetector {
    public static final int ALL = 0;
    public static final int JAPANESE = 1;
    public static final int CHINESE = 2;
    public static final int SIMPLIFIED_CHINESE = 3;
    public static final int TRADITIONAL_CHINESE = 4;
    public static final int KOREAN = 5;
    public static final int NO_OF_LANGUAGES = 6;
    public static final int MAX_VERIFIERS = 16;
    nsVerifier[] mVerifier;
    nsEUCStatistics[] mStatisticsData;
    nsEUCSampler mSampler = new nsEUCSampler();
    byte[] mState = new byte[16];
    int[] mItemIdx = new int[16];
    int mItems;
    int mClassItems;
    boolean mDone;
    boolean mRunSampler;
    boolean mClassRunSampler;

    public nsPSMDetector() {
        this.initVerifiers(0);
        this.Reset();
    }

    public nsPSMDetector(int langFlag) {
        this.initVerifiers(langFlag);
        this.Reset();
    }

    public nsPSMDetector(int aItems, nsVerifier[] aVerifierSet, nsEUCStatistics[] aStatisticsSet) {
        this.mClassRunSampler = aStatisticsSet != null;
        this.mStatisticsData = aStatisticsSet;
        this.mVerifier = aVerifierSet;
        this.mClassItems = aItems;
        this.Reset();
    }

    public void Reset() {
        this.mRunSampler = this.mClassRunSampler;
        this.mDone = false;
        this.mItems = this.mClassItems;

        for(int i = 0; i < this.mItems; this.mItemIdx[i] = i++) {
            this.mState[i] = 0;
        }

        this.mSampler.Reset();
    }

    protected void initVerifiers(int currVerSet) {
        boolean idx = false;
        int currVerifierSet;
        if(currVerSet >= 0 && currVerSet < 6) {
            currVerifierSet = currVerSet;
        } else {
            currVerifierSet = 0;
        }

        this.mVerifier = null;
        this.mStatisticsData = null;
        if(currVerifierSet == 4) {
            this.mVerifier = new nsVerifier[]{new nsUTF8Verifier(), new nsBIG5Verifier(), new nsISO2022CNVerifier(), new nsEUCTWVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier()};
            this.mStatisticsData = new nsEUCStatistics[]{null, new Big5Statistics(), null, new EUCTWStatistics(), null, null, null};
        } else if(currVerifierSet == 5) {
            this.mVerifier = new nsVerifier[]{new nsUTF8Verifier(), new nsEUCKRVerifier(), new nsISO2022KRVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier()};
        } else if(currVerifierSet == 3) {
            this.mVerifier = new nsVerifier[]{new nsUTF8Verifier(), new nsGB2312Verifier(), new nsGB18030Verifier(), new nsISO2022CNVerifier(), new nsHZVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier()};
        } else if(currVerifierSet == 1) {
            this.mVerifier = new nsVerifier[]{new nsUTF8Verifier(), new nsSJISVerifier(), new nsEUCJPVerifier(), new nsISO2022JPVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier()};
        } else if(currVerifierSet == 2) {
            this.mVerifier = new nsVerifier[]{new nsUTF8Verifier(), new nsGB2312Verifier(), new nsGB18030Verifier(), new nsBIG5Verifier(), new nsISO2022CNVerifier(), new nsHZVerifier(), new nsEUCTWVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier()};
            this.mStatisticsData = new nsEUCStatistics[]{null, new GB2312Statistics(), null, new Big5Statistics(), null, null, new EUCTWStatistics(), null, null, null};
        } else if(currVerifierSet == 0) {
            this.mVerifier = new nsVerifier[]{new nsUTF8Verifier(), new nsSJISVerifier(), new nsEUCJPVerifier(), new nsISO2022JPVerifier(), new nsEUCKRVerifier(), new nsISO2022KRVerifier(), new nsBIG5Verifier(), new nsEUCTWVerifier(), new nsGB2312Verifier(), new nsGB18030Verifier(), new nsISO2022CNVerifier(), new nsHZVerifier(), new nsCP1252Verifier(), new nsUCS2BEVerifier(), new nsUCS2LEVerifier()};
            this.mStatisticsData = new nsEUCStatistics[]{null, null, new EUCJPStatistics(), null, new EUCKRStatistics(), null, new Big5Statistics(), new EUCTWStatistics(), new GB2312Statistics(), null, null, null, null, null, null};
        }

        this.mClassRunSampler = this.mStatisticsData != null;
        this.mClassItems = this.mVerifier.length;
    }

    public abstract void Report(String var1);

    public boolean HandleData(byte[] aBuf, int len) {
        for(int i = 0; i < len; ++i) {
            byte b = aBuf[i];
            int j = 0;

            while(j < this.mItems) {
                byte st = nsVerifier.getNextState(this.mVerifier[this.mItemIdx[j]], b, this.mState[j]);
                if(st == 2) {
                    this.Report(this.mVerifier[this.mItemIdx[j]].charset());
                    this.mDone = true;
                    return this.mDone;
                }

                if(st == 1) {
                    --this.mItems;
                    if(j < this.mItems) {
                        this.mItemIdx[j] = this.mItemIdx[this.mItems];
                        this.mState[j] = this.mState[this.mItems];
                    }
                } else {
                    this.mState[j++] = st;
                }
            }

            if(this.mItems <= 1) {
                if(1 == this.mItems) {
                    this.Report(this.mVerifier[this.mItemIdx[0]].charset());
                }

                this.mDone = true;
                return this.mDone;
            }

            int nonUCS2Num = 0;
            int nonUCS2Idx = 0;

            for(j = 0; j < this.mItems; ++j) {
                if(!this.mVerifier[this.mItemIdx[j]].isUCS2() && !this.mVerifier[this.mItemIdx[j]].isUCS2()) {
                    ++nonUCS2Num;
                    nonUCS2Idx = j;
                }
            }

            if(1 == nonUCS2Num) {
                this.Report(this.mVerifier[this.mItemIdx[nonUCS2Idx]].charset());
                this.mDone = true;
                return this.mDone;
            }
        }

        if(this.mRunSampler) {
            this.Sample(aBuf, len);
        }

        return this.mDone;
    }

    public void DataEnd() {
        if(!this.mDone) {
            if(this.mItems == 2) {
                if(this.mVerifier[this.mItemIdx[0]].charset().equals("GB18030")) {
                    this.Report(this.mVerifier[this.mItemIdx[1]].charset());
                    this.mDone = true;
                } else if(this.mVerifier[this.mItemIdx[1]].charset().equals("GB18030")) {
                    this.Report(this.mVerifier[this.mItemIdx[0]].charset());
                    this.mDone = true;
                }
            }

            if(this.mRunSampler) {
                this.Sample((byte[])null, 0, true);
            }

        }
    }

    public void Sample(byte[] aBuf, int aLen) {
        this.Sample(aBuf, aLen, false);
    }

    public void Sample(byte[] aBuf, int aLen, boolean aLastChance) {
        int possibleCandidateNum = 0;
        int eucNum = 0;

        int j;
        for(j = 0; j < this.mItems; ++j) {
            if(this.mStatisticsData[this.mItemIdx[j]] != null) {
                ++eucNum;
            }

            if(!this.mVerifier[this.mItemIdx[j]].isUCS2() && !this.mVerifier[this.mItemIdx[j]].charset().equals("GB18030")) {
                ++possibleCandidateNum;
            }
        }

        this.mRunSampler = eucNum > 1;
        if(this.mRunSampler) {
            this.mRunSampler = this.mSampler.Sample(aBuf, aLen);
            if((aLastChance && this.mSampler.GetSomeData() || this.mSampler.EnoughData()) && eucNum == possibleCandidateNum) {
                this.mSampler.CalFreq();
                int bestIdx = -1;
                int eucCnt = 0;
                float bestScore = 0.0F;

                for(j = 0; j < this.mItems; ++j) {
                    if(this.mStatisticsData[this.mItemIdx[j]] != null && !this.mVerifier[this.mItemIdx[j]].charset().equals("Big5")) {
                        float score = this.mSampler.GetScore(this.mStatisticsData[this.mItemIdx[j]].mFirstByteFreq(), this.mStatisticsData[this.mItemIdx[j]].mFirstByteWeight(), this.mStatisticsData[this.mItemIdx[j]].mSecondByteFreq(), this.mStatisticsData[this.mItemIdx[j]].mSecondByteWeight());
                        if(eucCnt++ == 0 || bestScore > score) {
                            bestScore = score;
                            bestIdx = j;
                        }
                    }
                }

                if(bestIdx >= 0) {
                    this.Report(this.mVerifier[this.mItemIdx[bestIdx]].charset());
                    this.mDone = true;
                }
            }
        }

    }

    public String[] getProbableCharsets() {
        String[] ret;
        if(this.mItems <= 0) {
            ret = new String[]{"nomatch"};
            return ret;
        } else {
            ret = new String[this.mItems];

            for(int i = 0; i < this.mItems; ++i) {
                ret[i] = this.mVerifier[this.mItemIdx[i]].charset();
            }

            return ret;
        }
    }
}
